package com.szqhyxj.youxiangju.biz.card;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.realNameAuth.bankcard.BankList;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.RetrofitFacade;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

/**
 * XiaoPanYu
 * on 2018/10/31
 */
public class CardModel{

    public interface CardsService {
        @POST("user/bankCard/getall")
        Observable<BaseResult<List<Card>>> getCards();

        //获取用银行卡信息   "银行卡类型,1=储蓄卡，2=信用卡"
        @FormUrlEncoded
        @POST("user/bankCard/query")
        Observable<BaseResult<List<Card>>> getDeposit(@Field("cardType") String cardType
                                                ,@Field("token") RequestBody token);

        @FormUrlEncoded
        @POST("user/bankCard/querydetails")
        Observable<BaseResult<Card>> getCreditCard(@Field("cardId") String cardId);

        @FormUrlEncoded
        @POST("user/bankCard/querydetails")
        Observable<BaseResult<Card>> getDepositCard(@Field("cardId") String cardId);

        @FormUrlEncoded
        @POST("user/bankCard/edit")
        Observable<BaseResult> bankCreditCard(@Field("dueDate") String dueDate,@Field("billingDate") String billingDate,@Field("fixedAmount") String fixedAmount,@Field("cardId") String cardId);

        @POST("bank/trunk/cardapplication")
        Observable<BaseResult<List<BankList>>> bankList();

        @FormUrlEncoded
        @POST("user/bankCard/delete")
        Observable<BaseResult> deleteCard(@Field("cardId") String cardId);
    }

    //获取信用卡列表
    public static Observable<BaseResult<List<Card>>> getCards () {
        return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).getDeposit("2",getToken());
    }

    //获取储蓄卡
    public static Observable<BaseResult<List<Card>>> getDeposit () {
        return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).getDeposit("1",getToken());
    }

    //获取信用详情
    public static Observable<BaseResult<Card>> getcreditcard (String cardId) {
        return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).getCreditCard(cardId);
    }

    //获取信用详情
    public static Observable<BaseResult<Card>> getDepositCard (String cardId) {
        return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).getDepositCard(cardId);
    }

    //修改信用卡信息
    public static Observable<BaseResult> bankCreditCard (CardModel.CreditEntity creditEntity) {

        //return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).bankCreditCard(creditEntity);
        return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).bankCreditCard(creditEntity.getDueDate(),creditEntity.getBillingDate(),creditEntity.getFixedAmount(),creditEntity.getBankCardNo());

    }

    public static Observable<BaseResult<List<BankList>>> getBankList(){
        return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).bankList();
    }

    public static Observable<BaseResult> deleteCard (String cardNo) {
        return RetrofitFacade.Companion.getRetrofit().create(CardsService.class).deleteCard(cardNo);
    }

    @NonNull
    private static RequestBody getToken() {
        return RequestBody.create(MediaType.parse("text/plain"), TokenManager.INSTANCE.getMToken());
    }
    public static class CreditEntity{

        /**
         * dueDate : 还款日
         * billingDate : 账单日
         * fixedAmount : 固定额度
         * bankCardNo : 银行卡卡号
         */
        private String dueDate;
        private String billingDate;
        private String fixedAmount;
        private String cardId;

        public String getDueDate(){
            return dueDate;
        }

        public void setDueDate(String dueDate){
            this.dueDate = dueDate;
        }

        public String getBillingDate(){
            return billingDate;
        }

        public void setBillingDate(String billingDate){
            this.billingDate = billingDate;
        }

        public String getFixedAmount(){
            return fixedAmount;
        }

        public void setFixedAmount(String fixedAmount){
            this.fixedAmount = fixedAmount;
        }

        public String getBankCardNo(){
            return cardId;
        }

        public void setBankCardNo(String bankCardNo){
            this.cardId = bankCardNo;
        }
    }


    public static class Card implements Parcelable{

        /**
         * cardId : 5243
         * userId : 3
         * useraccount : 132456
         * btCode : 2
         * btName : 招商银行
         * username : 肖盼宇
         * idCard :
         * bankCardNo :
         * cardPhone :
         * cardType : 2
         * bankCardPic : http://192.168.1.137:test.jpg
         * imgUrl :http://192.168.1.137:test.jpg
         * cvn : 325
         * expiryDate : 03/22
         * dueDate : 1
         * billingDate : 15
         * fixedAmount : 10000
         * addDate : 2018-10-24 16:23:57
         */

        private String cardId;
        private String userId;
        private String useraccount;
        private String btCode;
        private String btName;
        private String username;
        private String idCard;
        private String bankCardNo;
        private String cardPhone;
        private String cardType;
        private String bankCardPic;
        private String cvn;
        private String expiryDate;
        private String dueDate;
        private String billingDate;
        private String fixedAmount;
        private String addDate;
        private String imgUrl;
        private String cardLogo;

        public String getCardLogo(){
            return cardLogo;
        }

        public void setCardLogo(String cardLogo){
            this.cardLogo = cardLogo;
        }

        public String getImgUrl(){
            return imgUrl;
        }

        public void setImgUrl(String imgUrl){
            this.imgUrl = imgUrl;
        }

        public String getCardId(){
            return cardId;
        }

        public void setCardId(String cardId){
            this.cardId = cardId;
        }

        public String getUserId(){
            return userId;
        }

        public void setUserId(String userId){
            this.userId = userId;
        }

        public String getUseraccount(){
            return useraccount;
        }

        public void setUseraccount(String useraccount){
            this.useraccount = useraccount;
        }

        public String getBtCode(){
            return btCode;
        }

        public void setBtCode(String btCode){
            this.btCode = btCode;
        }

        public String getBtName(){
            return btName;
        }

        public void setBtName(String btName){
            this.btName = btName;
        }

        public String getUsername(){
            return username;
        }

        public void setUsername(String username){
            this.username = username;
        }

        public String getIdCard(){
            return idCard;
        }

        public void setIdCard(String idCard){
            this.idCard = idCard;
        }

        public String getBankCardNo(){
            return bankCardNo;
        }

        public void setBankCardNo(String bankCardNo){
            this.bankCardNo = bankCardNo;
        }

        public String getCardPhone(){
            return cardPhone;
        }

        public void setCardPhone(String cardPhone){
            this.cardPhone = cardPhone;
        }

        public String getCardType(){
            return cardType;
        }

        public void setCardType(String cardType){
            this.cardType = cardType;
        }

        public String getBankCardPic(){
            return bankCardPic;
        }

        public void setBankCardPic(String bankCardPic){
            this.bankCardPic = bankCardPic;
        }

        public String getCvn(){
            return cvn;
        }

        public void setCvn(String cvn){
            this.cvn = cvn;
        }

        public String getExpiryDate(){
            return expiryDate;
        }

        public void setExpiryDate(String expiryDate){
            this.expiryDate = expiryDate;
        }

        public String getDueDate(){
            return dueDate;
        }

        public void setDueDate(String dueDate){
            this.dueDate = dueDate;
        }

        public String getBillingDate(){
            return billingDate;
        }

        public void setBillingDate(String billingDate){
            this.billingDate = billingDate;
        }

        public String getFixedAmount(){
            return fixedAmount;
        }

        public void setFixedAmount(String fixedAmount){
            this.fixedAmount = fixedAmount;
        }

        public String getAddDate(){
            return addDate;
        }

        public void setAddDate(String addDate){
            this.addDate = addDate;
        }

        @Override
        public int describeContents(){
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags){
            dest.writeString(this.cardId);
            dest.writeString(this.userId);
            dest.writeString(this.useraccount);
            dest.writeString(this.btCode);
            dest.writeString(this.btName);
            dest.writeString(this.username);
            dest.writeString(this.idCard);
            dest.writeString(this.bankCardNo);
            dest.writeString(this.cardPhone);
            dest.writeString(this.cardType);
            dest.writeString(this.bankCardPic);
            dest.writeString(this.cvn);
            dest.writeString(this.expiryDate);
            dest.writeString(this.dueDate);
            dest.writeString(this.billingDate);
            dest.writeString(this.fixedAmount);
            dest.writeString(this.addDate);
            dest.writeString(this.imgUrl);
            dest.writeString(this.cardLogo);
        }

        public Card(){
        }

        protected Card(Parcel in){
            this.cardId = in.readString();
            this.userId = in.readString();
            this.useraccount = in.readString();
            this.btCode = in.readString();
            this.btName = in.readString();
            this.username = in.readString();
            this.idCard = in.readString();
            this.bankCardNo = in.readString();
            this.cardPhone = in.readString();
            this.cardType = in.readString();
            this.bankCardPic = in.readString();
            this.cvn = in.readString();
            this.expiryDate = in.readString();
            this.dueDate = in.readString();
            this.billingDate = in.readString();
            this.fixedAmount = in.readString();
            this.addDate = in.readString();
            this.imgUrl = in.readString();
            this.cardLogo = in.readString();
        }

        public static final Parcelable.Creator<Card> CREATOR = new Parcelable.Creator<Card>(){
            @Override
            public Card createFromParcel(Parcel source){
                return new Card(source);
            }

            @Override
            public Card[] newArray(int size){
                return new Card[size];
            }
        };
    }
}
